﻿using System;
using System.Collections.Generic;
using System.Text;
using WordService.Data.IDAL;
using System.Data.SqlClient;
using WordService.DBUtil;
using System.Data;

namespace WordService.Data.DAL
{
    public sealed class ArticleDAL: IArticleDAL
    {
        private const String ARTICLE_INSERT = @""
            + "INSERT INTO BAS_ARTICLE(TITLE,FULLNAME,PUBLISHDATE,ISSN,CN,COLLECTION,CATEGORY,SUMMARY) VALUES "
            + "(@Title,@FullName,@PublishDate,@ISSN,@CN,@Collection,@Category,@Summary) SELECT @@IDENTITY AS ID;";
        private const String ARTICLE_EXIST = @""
            + "SELECT COUNT(*) FROM BAS_ARTICLE WHERE ISSN=@ISSN AND CN=@CN AND TITLE=@Title";
        private const String ARTICLE_UPDATE = @"";
        

        public Int64 InsertArticle(String title, String fullName, DateTime publishDate, String issn, String cn, String collection, String category, String summary)
        {
            // 初始化参数
            SqlParameter[] articleParms = new SqlParameter[] { 
                new SqlParameter("@Title",title),
                new SqlParameter("@FullName",fullName),
                new SqlParameter("@PublishDate",publishDate),
                new SqlParameter("@ISSN",issn),
                new SqlParameter("@CN",cn),
                new SqlParameter("@Collection",collection),
                new SqlParameter("@Category",category),
                new SqlParameter("@Summary",summary)
            };

            Int64 ID = 0L;
            using (SqlDataReader dr = DBUtility.ExecuteReader(
                DBUtility.ConnectionStr,
                CommandType.Text,
                ARTICLE_INSERT,
                articleParms))
            {
                while (dr.Read())
                {
                    ID = Convert.ToInt64(dr["ID"]);
                }
            }
            return ID;
        }

        public System.Data.CommandType CommandText { get; set; }
    }
}
